<!--
导师业务经费发放管理新增
-->
<template>
    <el-dialog :visible.sync="dialogFlag" :title="titles" width="500px" :close-on-click-modal="false" @close="closeFunc">
      <el-form :model="form" ref="form" label-suffix="：" :rules="rules" label-width="130px">
          <el-row :gutter="10">
              <el-col :span="24">
                  <el-form-item :label="$t('fundDistribution.sendYear')" prop="issueYear">
                    <el-select
                      v-model="form.issueYear"
                      style="width: 100%;"
                      :clearable="false"
                      filterable
                      :placeholder="$t('common.text.all')"
                      :disabled="objs.editFlag"
                      v-clearreadonly
                    >
                      <el-option
                          v-for="item in options.issueYear"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                      />
                    </el-select>
                  </el-form-item>
              </el-col>
              <el-col :span="24">
                  <el-form-item :label="$t('fundDistribution.stuNameOrNum')" prop="stuNo">
                    <el-select
                      v-model="form.stuNo"
                      :remote-method="selectStu"
                      :placeholder="$t('fundDistribution.stuNoPlaceholder')"
                      filterable
                      remote
                      style="width: 100%;"
                      :clearable="true"
                      :disabled="objs.editFlag"
                      @change="stuChange"
                      v-clearreadonly
                    >
                      <el-option
                        v-for="item in students"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                  </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label="$t('fundDistribution.tutorNameOrNum')" prop="tutorNo">
                  <el-input v-model="form.tutorNo" placeholder="" :readonly="true" :disabled="objs.editFlag"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label="$t('fundDistribution.standardFunding')" prop="fundsStandard">
                  <el-input v-model="form.fundsStandard" placeholder="" :readonly="true" :disabled="objs.editFlag"></el-input>
                  <div v-show="tipState" class="tips">{{$t('fundDistribution.standardFundingMsg')}}</div>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label="$t('fundDistribution.actualExpenses')" prop="actualFunds">
                  <el-input v-model="form.actualFunds" :placeholder="$t('common.pla.pleaseInput')" :maxlength="6"></el-input>
                </el-form-item>
              </el-col>
          </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFlag = false" >{{$t('message.cancel')}}</el-button>
        <el-button type="primary" @click="save" :disabled="submitState" >{{$t('message.confirm')}}</el-button>
      </span>
    </el-dialog>
</template>

<script>
import { getStuFundsStandard, distributionAdd, distributionUpdate, distributionGetDetail } from "common/src/api/fundManagement/standardSetting";
import commonMixins from 'common/src/views/practiceManagement/commonMixins';
import { findTeacherInfoList, findStuInfoListForSelect } from 'common/src/api/studentinfo';
export default {
    components: {},
    mixins: [commonMixins],
    props: {
      objs: {
        type: Object,
      },
      options: {
        type: Object,
      }
    },
    data() {
        return {
            form: {
              issueYear: '',
              stuNo: '',
              tutorNo: '',
              fundsStandard: '',
              actualFunds: '',
            },
            rules: {
              stuNo: [
                { required: true, message: this.$t('inputPlaceholder'), trigger: 'change' }
              ],
              actualFunds: [
                { required: true, validator: this.checkFund, trigger: 'change' }
              ]
            },
            dialogFlag: false,
            titles: '',
            students: [],
            tipState: false, // 标准经费提示信息显示状态
            submitState: false // 提交按钮状态
        };
    },
    computed: {

    },
    mounted() {
    },
    methods: {
      save() { // flag = edit 修改
        this.$refs.form.validate((valid) => {
          if (valid) {
            if (!this.objs.editFlag) { // 新增
              let param = _.cloneDeep(this.form);
              let tutoId = this.students.filter(v=> v.value === param.stuNo);
              param.tutorNo = tutoId.length > 0 ? tutoId[0].teacherId : '';
              distributionAdd(param).then(res=>{
                if (res.code === 200) {
                  this.$message.success(this.$t('message.addSuccess'));
                  this.dialogFlag = false;
                  this.$emit('emit:back');
                }
              });
            } else { // 编辑
              let param = {
                "id": this.form.id,
                "manageDeptId": this.form.manageDeptId,
                "issueYear": this.form.issueYear,
                "stuNo": this.form.stuNo,
                "stuName": this.form.stuName,
                "eduLevel": this.form.eduLevel,
                "eduMode": this.form.eduMode,
                "degreeType": this.form.degreeType,
                "studyForm": this.form.studyForm,
                "faculty": this.form.faculty,
                "profession": this.form.profession,
                "category": this.form.category,
                "tutorNo": this.form.tutorNo,
                "tutorName": this.form.tutorName,
                "fundsStandard": this.form.fundsStandard,
                "actualFunds": this.form.actualFunds,
                "isAdjust": this.form.isAdjust,
                "enableStatus": this.form.enableStatus,
                "deleteStatus": this.form.deleteStatus
              };
              distributionUpdate(param).then(res=>{
                if (res.code === 200) {
                  this.$message.success(this.$t('achievementApply.editSuccessfully'));
                  this.dialogFlag = false;
                  this.$emit('emit:back');
                }
              });
            }
          }
        });
      },
      // 根据id获取规则详情
      getDetail (id) {
        distributionGetDetail(id).then(res=>{
          if (res.code === 200) {
            this.form = res.data;
            this.selectStu(res.data.stuNo);
          }
        });
      },
      // 查询学生信息接口
      selectStu (query) {
        if (!query) return;
        const data = {
          name: query,
          pageNum_: 1,
          pageSize_: 50
        }
        this.loading = true
        findStuInfoListForSelect(data)
          .then(res => {
            this.loading = false
          if (res.code == 200) {
              this.students = this.transData(res.data.list, 'name', 'studentId', 'teacherCodeAndName', 'teacherId')
            } else throw res.code
          })
          .catch(error => {
              this.loading = false
              this.$message({
                  type: 'error',
                  message: '查询用户出错'
              })
          })
      },
      transData(list, name, code, teacherCodeAndName, teacherId) {
        return list.map((item, index, array) => {
          return {
            label: item[name] + '(' + item[code] + ')',
            value: item[code],
            teacherId: item[teacherId],
            teacherCodeAndName: item[teacherCodeAndName]
          }
        })
      },
      // 学生信息改变
      stuChange(value) {
        if (!value || value === '') {
          this.tipState = false;
          this.submitState = false;
          this.form.tutorNo = '';
          return;
        }
        let stuInfo = this.students.filter(v=> v.value === value)[0];
        this.form.tutorNo = stuInfo.teacherCodeAndName;
        this.getStuInfo(value);
      },
      // 获取学生对应的经费标准
      getStuInfo(value) {
        getStuFundsStandard(value).then(res=>{
          if (res.code == 200) {
            let dataInfo = res.data;
            this.form.fundsStandard = '';
            this.form.actualFunds = '';
            if (!dataInfo || dataInfo.length === 0) {
              this.tipState = true;
              this.submitState = false;
            } else if (dataInfo.length > 1) {
              this.tipState = false;
              this.submitState = true;
              let stuInfo = this.students.filter(v=> v.value === value)[0];
              if (stuInfo.label) {
                this.$message.error(this.$t('fundDistribution.student')+stuInfo.label+this.$t('fundDistribution.moreRules'));
              }
            } else {
              this.tipState = false;
              this.submitState = false;
              this.form.fundsStandard = dataInfo[0].fund;
              this.form.actualFunds = dataInfo[0].fund;
            }
          }
        });
      },
      closeFunc() {
        this.$refs.form.resetFields();
        this.tipState = false; // 标准经费提示信息显示状态
        this.submitState = false; // 提交按钮状态
      },
      checkFund(rule, value, callback) {
        let regTest = /^\d+(\.\d{0,2})?$/;
        if (!value || value === '') {
          return callback(this.$t('inputPlaceholder'));
        }
        if (!regTest.test(value)) {
          return callback(this.$t('standardSetting.numTips'));
        }
        return callback();
      },
    }
};
</script>

<style scoped lang="scss">
.tips {
  color: #F56C6C;
  font-size: 12px;
  line-height: 15px;
}
</style>
